package org.cloudplatform.common.test.model;

import java.io.Serializable;


public class Result<T> implements Serializable {


    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public final static int SUCCESS = 200;
    /**
     * 业务逻辑错误
     */
    private final static int FAILED = 400;

    /**
     * 访问受限（无权限）
     */
    public final static int FORBIDDEN = 403;

    /**
     * 未认证
     */
    public final static int NOT_AUTHEN = 401;

    /**
     * 代码异常
     */
    private final static int ERROR = 500;

    /**
     * 操作状态: 200-成功; 400-业务失败; 401-未认证;403-访问受限;500-服务器内部错误
     */
    private int code;


    /**
     * 操作提示
     */
    private String message;


    /**
     * 返回数据
     */
    private T data;

    /**
     * 服务处理时间
     */
    private Long timestamp; /*时间戳*/


    /**
     * 处理成功
     */
    public static <T> Result<T> succeed(T data) {
        Result<T> json = new Result<>();
        json.setData(data).setCode(SUCCESS).setMessage("操作成功");
        return json;
    }

    /**
     * 处理成功
     */
    public static <T> Result<T> succeed(String msg) {
        Result<T> json = new Result<>();
        json.setCode(SUCCESS).setMessage(msg);
        return json;
    }


    /**
     * 处理成功
     */
    public static <Void> Result<Void> succeed() {
        Result<Void> json = new Result<>();
        json.setCode(SUCCESS).setMessage("操作成功");
        return json;
    }

    /**
     * 处理失败
     */
    public static <Void> Result<Void> failed(String msg) {
        return new Result<Void>().setCode(FAILED).setMessage(msg);
    }


    public static <Void> Result<Void> forbidden() {
        return new Result<Void>().setCode(FORBIDDEN).setMessage("操作受限");
    }

    public static <Void> Result<Void> forbidden(String message) {
        return new Result<Void>().setCode(FORBIDDEN).setMessage(message);
    }

    public static <Void> Result<Void> unauthorized() {
        return new Result<Void>().setCode(NOT_AUTHEN).setMessage("用戶未登录");
    }


    /**
     * 获取一个成功正文数据 当请求出现异常时会抛出异常信息
     */
    public T dispose() {
        if (resultIsSuccess()) {
            return this.getData();
        } else if (resultIsFailed()) {
            throw new RuntimeException(this.message);
        } else if (resultIsError()) {
            throw new RuntimeException(this.message);
        } else {
            throw new RuntimeException(this.message);
        }
    }


    public String getMessage() {
        return message;
    }

    public Result<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public int getCode() {
        return code;
    }

    public Result<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }


    public Long getTimestamp() {
        return timestamp;
    }

    public Result<T> setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    /**
     * 请求响应是否成功
     */
    public boolean resultIsSuccess() {
        return this.code == SUCCESS;
    }

    /**
     * 请求响应是否失败
     */
    public boolean resultIsFailed() {
        return this.code == FAILED;
    }

    /**
     * 请求是否出现内部错误
     */
    public boolean resultIsError() {
        return this.code == ERROR;
    }

    /**
     * 请求是否出现操作受限
     */
    public boolean resultIsForbidden() {
        return this.code == FORBIDDEN;
    }

    /**
     * 请求是否未认证
     */
    public boolean resultIsUnauthorized() {
        return this.code == NOT_AUTHEN;
    }

}
